/****************************************************************
 ** Product  :   HP Subscriber Network Application Policy
 ** Module   :   gr-manager
 ** Date: Apr 10, 2015               
 ** Author: Hua Zhen
 ** (C) Copyright 2015, Hewlett-Packard Company, All Rights Reserved.
 ** This software is the proprietary information of HP, Inc.  
 ** Use is subject to license terms.
 *****************************************************************
 ** Description:   
 **     Check DB status, and pause the DB cluster if running
 **    
 ****************************************************************/
package com.hp.snap.gr.action.impl;

import com.hp.snap.gr.exception.ActionException;
import com.hp.snap.gr.model.Database;
import com.hp.snap.gr.model.jaxb.ActionType;
import com.hp.snap.gr.model.jaxb.GeneralActionType;
import com.hp.snap.gr.model.jaxb.ParameterType;
import com.hp.snap.gr.utils.Constants;
import com.hp.snap.gr.utils.RESTfulUtils;
import com.hp.snap.gr.utils.Utils;
import com.hp.snap.gr.utils.VoltDBController;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * 
 * Check DB status, and pause the DB cluster if running
 * 
 * There is one DB_INSTANCE_NAME parameter need to be configured.
 * DB_INSTANCE_NAME: The VoltDB DB instance name, such as: spr_db, session_db, lb_db.
 * 
 * <P>
 * Version: 3.3 <br>
 * Author: Hua Zhen Date: Apr 10, 2015
 * 
 * </P>
 * 
 **/
public class CheckVoltDBStatusAndPauseActionHandler extends AbstractActionHandler {
    private static Logger logger = LoggerFactory.getLogger(CheckVoltDBStatusAndPauseActionHandler.class);

    public void execute(ActionType action,
                        Map<String, Object> actionPlanInputs,
                        Map<String, Object> actionPlanOutputs) throws ActionException {
        GeneralActionType actionType = (GeneralActionType) action;
        List<ParameterType> parameterTypes = actionType.getParameter();
        Map<String, String> parameterMap = Utils.contructParamMap(parameterTypes);

        /**
         * get the db instance name, which should be configured in: /SNAP/GR/<DEPLOY_NAME>/VOLTDB/<DB_INSTANCE_NAME>, e.g.
         * /SNAP/GR/Site_A1/VOLTDB/session_db]
         */
        String dbInstName = parameterMap.get("DB_INSTANCE_NAME");
        if (StringUtils.isEmpty(dbInstName)) {
            throw new ActionException("The parameter [DB_INSTANCE_NAME] should be configured!");
        }

        // find out the target database profile
        Database database = RESTfulUtils.getVoltDBDatabase(dbInstName);

        if (database == null) {
            throw new ActionException("The database instance is not exist: " + dbInstName);
        }

        String status = database.getStatus();
        if (logger.isDebugEnabled()) {
            logger.debug("[CheckVoltDBStatusAndPauseActionHandler] db status" + status);
        }
        if (Constants.VDB_STATUS_OFFLINE.equals(status)) {

            logger.info("The DB Instance [{}] is stopped, and cannot be paused!", dbInstName);

        } else if (Constants.VDB_STATUS_ONLINE.equals(status)) {
            if (logger.isDebugEnabled()) {
                logger.debug("pauseing db name ={" + database.getName() + "}");
            }
            VoltDBController.pauseDatabase(database);
            logger.info("Pause the DB Instance successful, Name: {}, ID: {}", dbInstName, database.getId());
        } else {
            logger.warn("The database is in status [{}], and do not process!", status);
        }
    }

}
